package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.soap.Detail;
import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper detailMapper;
    @Autowired
    private StrategyContentMapper contentMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyDetail> list = detailMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(StrategyDetail detail) {
        if (detail.getSequence()==null) {
            int count = detailMapper.selectMaxSequence(detail.getCatalog().getId());
            detail.setSequence(++count);
        }

        if (detail.getState()== StrategyDetail.STATE_RELEASE) {
            detail.setReleaseTime(new Date());
        }
        StrategyContent content = detail.getContent();

        if (detail.getId()==null) {
            detail.setCreateTime(new Date());
            detailMapper.insert(detail);
            content.setId(detail.getId());
            contentMapper.insert(content);
        }else{
            detailMapper.updateByPrimaryKey(detail);
            content.setId(detail.getId());
            contentMapper.updateByPrimaryKey(content);
        }
    }

    @Override
    public StrategyContent getContentByDetailId(Long detailId) {

        return contentMapper.selectByPrimaryKey(detailId);
    }

    @Override
    public StrategyDetail selectDetailByCatalogId(Long id) {
        StrategyDetail detail = detailMapper.selectByPrimaryKey(id);
        StrategyContent content = contentMapper.selectByPrimaryKey(id);
        detail.setContent(content);
        return detail;
    }
}
